<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>写入文件（可安全用于二进制文件）</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.fwrite" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fwrite</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">fwrite</span> &mdash; <span class="dc-title">写入文件（可安全用于二进制文件）</span></p>

 </div>
 
  <div class="refsect1 description" id="refsect1-function.fwrite-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fwrite</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
  ] ) : <span class="type">int</span></div>

  <p class="simpara">
   <span class="function"><strong>fwrite()</strong></span> 把 <code class="parameter">string</code> 的内容写入
   文件指针 <code class="parameter">handle</code> 处。 
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.fwrite-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">handle</code></dt>

     <dd>

      <p class="para">文件系统指针，是典型地由
<span class="function"><a href="fopen.html" class="function">fopen()</a></span> 创建的 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>(资源)。</p>
     </dd>

    
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       The string that is to be written.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       如果指定了
   <code class="parameter">length</code>，当写入了
   <code class="parameter">length</code> 个字节或者写完了 <code class="parameter">string</code>
   以后，写入就会停止，视乎先碰到哪种情况。
      </p>
      <p class="para">
        注意如果给出了
   <code class="parameter">length</code>
   参数，则 <a href="info.configuration.html#ini.magic-quotes-runtime" class="link">magic_quotes_runtime</a>
   配置选项将被忽略，而
   <code class="parameter">string</code> 中的斜线将不会被抽去。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
  <div class="refsect1 returnvalues" id="refsect1-function.fwrite-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="simpara">
   <span class="function"><strong>fwrite()</strong></span> 返回写入的字符数，出现错误时则返回 <strong><code>FALSE</code></strong> 。
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.fwrite-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Writing to a network stream may end before the whole string is written.
    Return value of <span class="function"><strong>fwrite()</strong></span> may be checked:
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">fwrite_stream</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$written&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$written&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);&nbsp;</span><span style="color: #0000BB">$written&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">$fwrite</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fwrite&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$written</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$fwrite&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$written</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$written</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </p>
  </p></blockquote>
  
   <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    在区分二进制文件和文本文件的系统上（如 Windows）
    打开文件时，<span class="function"><a href="fopen.html" class="function">fopen()</a></span> 函数的 mode 参数要加上 &#039;b&#039;。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If <code class="parameter">handle</code> was <span class="function"><a href="fopen.html" class="function">fopen()</a></span>ed in
    append mode, <span class="function"><strong>fwrite()</strong></span>s are atomic (unless the size of
    <code class="parameter">string</code> exceeds the filesystem&#039;s block size, on some
    platforms, and as long as the file is on a local filesystem). That is,
    there is no need to <span class="function"><a href="flock.html" class="function">flock()</a></span> a resource before calling
    <span class="function"><strong>fwrite()</strong></span>; all of the data will be written without
    interruption.
   </p>
  </p></blockquote>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If writing twice to the file pointer, then the data will be appended to 
    the end of the file content:
        <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'data.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'23'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;content&nbsp;of&nbsp;'data.txt'&nbsp;is&nbsp;now&nbsp;123&nbsp;and&nbsp;not&nbsp;23!<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
        </div>

   </p>
  </p></blockquote>
 </div>

 
  <div class="refsect1 examples" id="refsect1-function.fwrite-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2810">
    <p><strong>Example #1 一个简单的 <span class="function"><strong>fwrite()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test.txt'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$somecontent&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"添加这些文字到文件\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;首先我们要确定文件存在并且可写。<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">is_writable</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">))&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;在这个例子里，我们将使用添加模式打开$filename，<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;因此，文件指针将会在文件的末尾，<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;那就是当我们使用fwrite()的时候，$somecontent将要写入的地方。<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"不能打开文件&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;将$somecontent写入到我们打开的文件中。<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$somecontent</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"不能写入到文件&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"成功地将&nbsp;</span><span style="color: #0000BB">$somecontent</span><span style="color: #DD0000">&nbsp;写入到文件</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /><br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"文件&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #DD0000">&nbsp;不可写"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.fwrite-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - 读取文件（可安全用于二进制文件）</span></li>
    <li class="member"><span class="function"><a href="fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
    <li class="member"><span class="function"><a href="fsockopen.html" class="function" rel="rdfs-seeAlso">fsockopen()</a> - 打开一个网络连接或者一个Unix套接字连接</span></li>
    <li class="member"><span class="function"><a href="popen.html" class="function" rel="rdfs-seeAlso">popen()</a> - 打开进程文件指针</span></li>
    <li class="member"><span class="function"><a href="file_get_contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - 将整个文件读入一个字符串</span></li>
   </ul>
  </p>
 </div>

 
</div></div></div></body></html>